

package tables

import (
	"fmt"
	"game/game-design/loader"
	"strings"
	ministruct "sanguo/sanguo-design/src/ministruct"
)


var GlobalTable *GlobalTableStruct = &GlobalTableStruct{}

type GlobalTableStruct struct {
	loader.DesignTable[GlobalCfg]
	
	idxSubIdIndex map[string]*GlobalCfg
	
	
	
	IntExampleParam int32 //数字常量例子
	StringExampleParam string //字符常量例子
	AAA []ministruct.Reward //多奖励常量例子
	BBB ministruct.Reward //单奖励常量列子
	BagBuyLimit int32 //购买次数的上限
	bagFreeNum int32 //免费次数的上限
	FriendMaxNum int32 //好友最大上限
}

func (t *GlobalTableStruct) FileName() string {
	return "0Q全局常量表"
}

// 从索引中获取对象，获取不到会抛异常
func (t *GlobalTableStruct) GetInIdxSubIdIndex(
	idx int32, subId int32,
) *GlobalCfg {
	key :=
		fmt.Sprint(idx)+ fmt.Sprint(subId)
	
	cfg, ok := t.idxSubIdIndex[key]
	if !ok {
		panic("GlobalTable when GetInIdxSubIdIndex use key not found " + key)
	}
	return cfg
}

// 从索引中查找对象，获取不到会返回null
func (t *GlobalTableStruct) FindInIdxSubIdIndex(
	idx int32, subId int32,
) (*GlobalCfg, bool) {
	key :=
		fmt.Sprint(idx)+ fmt.Sprint(subId)
	
	cfg, ok := t.idxSubIdIndex[key]
	return cfg, ok
}

// 加载idxSubIdIndex索引
func (t *GlobalTableStruct) loadIdxSubIdIndex() {
	idxSubIdIndex := make(map[string]*GlobalCfg)
	for _, v := range t.All() {
		keys := []string {
			fmt.Sprint(v.idx),
			fmt.Sprint(v.subId),
		}
		key := strings.Join(keys, "_")
		_, exist := idxSubIdIndex[key]
		if exist {
			fmt.Println(t.FileName(), " found duplicate key ", key, " in index ", "idxSubIdIndex")
			continue
		}
		idxSubIdIndex[key] = v
	}
	t.idxSubIdIndex = idxSubIdIndex
}



func (t *GlobalTableStruct) LoadAutoGenerate() {
	//加载索引
	t.loadIdxSubIdIndex()

	
	//构建常量
	constMap := make(map[string]string)
	for _, cfg := range t.All() {
		constMap[cfg.GetKeyVal()] = cfg.GetValue();
	}
	loader.LoadConst(t, constMap);
}



type GlobalCfg struct {
	idx int32 //功能ID
	subId int32 //子序号
	comment string //备注
	keyVal string //常量主键
	typeVal string //常量类型
	value string //常量值
	tw_value string //常量值
}
// 功能ID
func (c *GlobalCfg) GetIdx() int32 {
	return c.idx
}
// 子序号
func (c *GlobalCfg) GetSubId() int32 {
	return c.subId
}
// 备注
func (c *GlobalCfg) GetComment() string {
	return c.comment
}
// 常量主键
func (c *GlobalCfg) GetKeyVal() string {
	return c.keyVal
}
// 常量类型
func (c *GlobalCfg) GetTypeVal() string {
	return c.typeVal
}
// 常量值
func (c *GlobalCfg) GetValue() string {
	return c.value
}
// 常量值
func (c *GlobalCfg) GetTw_value() string {
	return c.tw_value
}

func GlobalTableStructNoUse() {
	fmt.Println(ministruct.Cost{})
}
